// 配置分页插件的目录
layui.extend({
    customPage: '../lib/customPage/custom-page',
    apiconfig: '../public/apiconfig'
});
layui.use(['form',"table","laypage","customPage","laydate"], function () {
    var form = layui.form,
        table = layui.table,
        laypage = layui.laypage,
        laydate = layui.laydate,
        customPage = layui.customPage //自定义分页插件
        element = layui.element;
        var mainTop=$(".main_top").outerHeight()
        $(".tableMb").height(window.screen.height-(230+mainTop+10));
        // 数据字典获取值--------------
        workSheet(form,'questionType',"work_sheet_type");
        
        form.on('select(questionType)', function(data){
            workSheetOnSelect(form,"work_sheet_type",data.value);
        })
        createDept(form,'createDept');
          // 数据字典获取值--------------
        $.ajaxSetup({ cache: false })
        var searchstr={
            'statu':$("#statu option:selected").val()
        }
        customPageFun(searchstr);
        
    table.render({
        elem: '#tabList'
        ,title: '用户表'
        ,toolbar:'<div><a class="layui-btn layui-btn-radius bao_loginbtnLO" lay-event="exposrt" id="download">导出</a></div>'
        ,defaultToolbar: []
        // ,page: true //开启分页
        ,cols: [
            [{ type: 'checkbox', title: '选择',  width: 30},
            { type: 'numbers', title: '序号', width: 40},
            {field: 'workSheetCode',title: '工单编号',align:"center",width:150}, 
            {field: 'questionType', title: '问题类型',align:"center" }, 
            {field: 'questionContent', title: '问题内容',align:"center" }, 
            {field: 'createDate', title: '提交时间',width:150,align:"center"}, 
            {field: 'workSheetContacts', title: '联系人' ,align:"center"}, 
            {field: 'createDept', title: '处室',align:"center" }, 
            {field: 'workSheetPhone', title: '电话',width:150,align:"center"}, 
            {field: 'priority', title: '优先级',align:"center" , templet:function(d){
                if(d.priority=='低'){
                    return "低"
                }else if(d.priority=='高'){
                    return '<span class="priorityColor">'+d.priority+'</span>'
                }else if(d.priority=='紧急'){
                    return '<span class="priorityColor">'+d.priority+'</span>'
                }else{
                    return ''
                }
            }}, 
            {field: 'statu', title: '状态',align:"center"}, 
            {field: 'detailds', title: '详情',align:"center", width: 80,templet:function(){
               return '<a class="layui-btn layui-btn-xs" data-title="查看" lay-event="LisyJobsLook">查看</a>'
            }}
            ]
        ],
        done: function(data) {
        }
    }); 
     //执行示例 如何采用 table.reload 方法分页的渲染放到 table之后
    function customPageFun(searchstr){
        customPage.render({
            elem: '#page',
            layout: ['skipFirst', 'prev', 'skip', 'next', 'skipLast', 'count'],
            url: 'gt/opm/workSheet/admin/page',
            method: 'GET',
            request: {
                pageName: 'pageNum',
                limitName: 'pageSize'
            },
            where: {
                workSheetCode:searchstr.workSheetCode,
                questionContent:searchstr.questionContent,
                questionType:searchstr.questionType,
                statu:searchstr.statu,
                priority:searchstr.priority,
                endDate:searchstr.endDate,
                startDate:searchstr.startDate,
                workSheetContacts:searchstr.workSheetContacts,
                createDept:searchstr.createDept
            },
            parseData:function(res){
                return {
                    "code":res.code, //解析接口状态
                    "flag":res.flag, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "data": res.data.rows, //解析数据列表
                    "total":res.data.total
                };
            },
            done: function(res, curr, data) {
                if(res.flag==true){
                    table.reload('tabList', {
                        data: res.data,
                        limit: res.size
                    });
                }else{
                    layer.alert(res.msg)
                }
                
            }
        });
    }
    // 监听列表点击事件
    table.on('tool(tabList)', function(obj){
       if(obj.event=="LisyJobsLook"){
          window.parent.xadmin.add_tab('工单管理详情','views/OrderDetailwork.html','',obj.data.id)
       }
    })
     // 监听toolbar点击事件
     table.on('toolbar(tabList)', function(obj){
        // var checkStatus = table.checkStatus('tabList');
        // // 导出
        if(obj.event=="exposrt"){
            if (table.exportCols.length > 0 && table.cache.tabList.length > 0) {
            // if(checkStatus.data!=''){
                var dateL=$("#pushTime").val()
                var startTime=dateL.substr(0,10),
                endTime=dateL.substr(13,dateL.length);
                var priority=$("#priority option:selected").val(),
                questionType=$("#questionType option:selected").val(),
                questionContent=$("#questionContent option:selected").val(),
                statut=$("#statu option:selected").val(),
                workSheetCode=$("#workSheetCode").val(),
                workSheetContacts=$("#workSheetContacts").val(),
                createDept=$("#createDept option:selected").val(),
                endDate=endTime;
                startDate=startTime;
                var url = "gt/opm/workSheet/admin/export?workSheetCode=" + workSheetCode +
                    "&questionType=" + questionType + "&startDate=" + startDate +
                    "&endDate=" + endDate + "&priority=" + priority +
                    "&questionContent=" + questionContent + "&workSheetContacts=" + workSheetContacts +
                    "&createDept=" + createDept + "&statu=" + statut ;
                $("#download").attr("href", url);
            } else {
                layer.msg("暂无可导出数据");
            }
        }
     })
    //日期范围
    laydate.render({
        elem: '#pushTime'
        ,range: true
    });
    var active={
        search:function(){
            var dateL=$("#pushTime").val()
            var startTime=dateL.substr(0,10),
            endTime=dateL.substr(13,dateL.length);
            searchstr.workSheetCode=$("#workSheetCode").val();
            searchstr.workSheetContacts=$("#workSheetContacts").val();
            searchstr.priority=$("#priority option:selected").val();
            searchstr.questionType=$("#questionType option:selected").val();
            searchstr.questionContent=$("#questionContent option:selected").val();
            searchstr.createDept=$("#createDept option:selected").val();
            // searchstr.statu=$("#statu option:selected").val();
            searchstr.endDate=endTime;
            searchstr.startDate=startTime;
            customPageFun(searchstr)
        }
    }
    $(".bao_loginbtn").on('click',function(data){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    })
   
})